import { decompress } from "./decompress";

const keyStrBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

export type Dictionary = Record<string, number>;
export type DictionaryCollection = Record<string, Dictionary>;

const baseReverseDic: DictionaryCollection = {};

export function getBaseValue(alphabet: string, character: string): number {
    if (!baseReverseDic[alphabet]) {
        baseReverseDic[alphabet] = {};
        for (let i = 0; i < alphabet.length; i++) {
            baseReverseDic[alphabet][alphabet.charAt(i)] = i;
        }
    }
    return baseReverseDic[alphabet][character];
}

export function decompressFromBase64(input: string | null) {
    if (input == null) return "";
    if (input == "") return null;

    return decompress(input.length, 32, (index) => getBaseValue(keyStrBase64, input.charAt(index)));
}
